home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 8 / Mac Magazin and MacEasy Magazine CD - Issue 08.iso / Sharewarebibliothek / Musik / SoundEffects ƒ / SoundEffects Developer’s Kit / Gain ƒ / Gain.c next >
C/C++ Source or Header  |  1994-11-21  |  3KB  |  141 lines

  1. #include "Glue.h"
  2. #include "Gain.h"
  3. #include "ModGetSetSamples.h"
  4.  
  5.  
  6. pascal OSErr effect(ModParamsPtr modInfo, GluePtr glue, ModSettingsHandle *prefs)
  7. {
  8.     Handle            channelHand;
  9.     StringHandle    progressString;
  10.     Ptr                loopPtr, startPtr, endPtr;
  11.     signed long        mySample, min, max;
  12.     unsigned long    start, end;
  13.     unsigned long    channelSize, processedSamples, samplesToProcess;
  14.     short            myGain;
  15.     short            frameSize, timeToCallProgress;
  16.     short            channel;
  17.     OSErr            progressResult = kModNoError;
  18.     OSErr            result = kModCancel;
  19.     
  20.     // effect can receive in prefs either nil or a valid handle. If the effect does
  21.     // not have any settings, prefs will be nil and we won’t do anything with it.
  22.     
  23.     UseResFile(modInfo->refNum);
  24.     
  25.     
  26.     // read the settings.
  27.     
  28.     myGain = (**prefs)->gain;
  29.     DisposHandle((Handle)*prefs);
  30.     *prefs = 0L;
  31.     
  32.     
  33.     // let’s load the string we’ll show in the progress window.
  34.     
  35.     progressString = (StringHandle)Get1Resource('STR ', 128);
  36.     if (progressString == 0L)
  37.         return kModCouldntLoadMyRes;
  38.     
  39.     
  40.     // set up the progress window.
  41.     
  42.     (*glue->ModSetupProgress)(modInfo, &timeToCallProgress, progressString);
  43.     ReleaseResource((Handle)progressString);
  44.     
  45.     
  46.     // if the selection is empty, select the whole channels.
  47.     
  48.     if (modInfo->selSt == modInfo->selEnd)
  49.     {
  50.         modInfo->selSt = 0L;
  51.         (*glue->ModMaxRelChSize)(&modInfo, &modInfo->selEnd);
  52.     }
  53.     
  54.     
  55.     (*glue->ModGetSampleValueLimits)(modInfo->bps, &min, &max);
  56.     frameSize = modInfo->bps/8 + (modInfo->bps%8 != 0);
  57.     samplesToProcess = (*glue->GetBytesToProcess)(modInfo) / frameSize;
  58.     processedSamples = 0L;
  59.     
  60.     
  61.     for (channel = modInfo->firstSelCh-1; channel < modInfo->lastSelCh; channel++)
  62.     {
  63.         start = modInfo->selSt;
  64.         end = modInfo->selEnd;
  65.         
  66.         channelSize = (*modInfo->hands)[channel].size;
  67.         if ( start > channelSize )
  68.             start = channelSize;
  69.         if ( end > channelSize )
  70.             end = channelSize;
  71.         
  72.         start -= start%frameSize;
  73.         end -= end%frameSize;
  74.         
  75.         if (start != end)
  76.         {
  77.             channelHand = (*modInfo->hands)[channel].chan;
  78.             HLock(channelHand);
  79.             
  80.             startPtr = *channelHand + start;
  81.             endPtr = *channelHand + end;
  82.             
  83.             if (frameSize == 1)
  84.             {
  85.                 for (loopPtr = startPtr; loopPtr < endPtr; loopPtr++, processedSamples++)
  86.                 {
  87.                     mySample = GetSample8(loopPtr);
  88.                     
  89.                     mySample = mySample*myGain/100;
  90.                     
  91.                     if (mySample & 0x80000000)    // Equivalent to “if mySample < 0L”
  92.                     {
  93.                         if (mySample < min)
  94.                             mySample = min;
  95.                     }
  96.                     else if (mySample > max)
  97.                         mySample = max;
  98.                     
  99.                     SetSample8(loopPtr, mySample);
  100.                     
  101.                     if (timeToCallProgress)
  102.                         if (progressResult = (*glue->ModShowProgress)(processedSamples, samplesToProcess, &timeToCallProgress, false))
  103.                             break;
  104.                 }
  105.             }
  106.             else
  107.             {
  108.                 for (loopPtr = startPtr; loopPtr < endPtr; loopPtr += frameSize, processedSamples++)
  109.                 {
  110.                     mySample = GetSample16(loopPtr, modInfo->bps);
  111.                     
  112.                     mySample = mySample*myGain/100;
  113.                     
  114.                     if (mySample & 0x80000000)    // Equivalent to “if mySample < 0L”
  115.                     {
  116.                         if (mySample < min)
  117.                             mySample = min;
  118.                     }
  119.                     else if (mySample > max)
  120.                         mySample = max;
  121.                     
  122.                     SetSample16(loopPtr, mySample, modInfo->bps);
  123.                     
  124.                     if (timeToCallProgress)
  125.                         if (progressResult = (*glue->ModShowProgress)(processedSamples, samplesToProcess, &timeToCallProgress, false))
  126.                             break;
  127.                 }
  128.             }
  129.             
  130.             HUnlock(channelHand);
  131.             result = kModNoError;
  132.         }
  133.         
  134.         if (progressResult)
  135.             break;
  136.     }
  137.     
  138.     (*glue->ModCloseProgress)();
  139.     
  140.     return result;
  141. }